/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package aptech.semester.code.gui;


import aptech.semester.code.data.PositionList;
import aptech.semester.code.entity.PositionEntity;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author vi
 */
public class Position extends javax.swing.JInternalFrame {
        Connection con;
        Statement stmt;
        ResultSet rs;
    /**
     * Creates new form Position
     */
    public Position(JFrame mainFrame) {
        initComponents();
        initControl();
        txtPosName.setEditable(false);
        txtDes.setEditable(false);
        txtBonus.setEditable(false);
        btnOk.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtPosName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtBonus = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtDes = new javax.swing.JTextArea();
        jPanel3 = new javax.swing.JPanel();
        btnOk = new javax.swing.JButton();
        btnDel = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        btnChange = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbPosition = new javax.swing.JTable();

        jButton1.setText("jButton1");

        setClosable(true);
        setTitle("Posistion");
        setPreferredSize(new java.awt.Dimension(800, 600));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Details"));

        jLabel1.setText("Position Name");

        txtPosName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPosNameActionPerformed(evt);
            }
        });

        jLabel2.setText("Bonus Salary");

        jLabel3.setText("Description");

        txtDes.setColumns(20);
        txtDes.setRows(5);
        jScrollPane2.setViewportView(txtDes);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(txtPosName, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtBonus, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE))
                    .addComponent(jScrollPane2))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtPosName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(txtBonus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Command"));

        btnOk.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/checked.png"))); // NOI18N
        btnOk.setText("OK");
        btnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkActionPerformed(evt);
            }
        });

        btnDel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/btn_cancel.png"))); // NOI18N
        btnDel.setText("Delete");
        btnDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDelActionPerformed(evt);
            }
        });

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/add.png"))); // NOI18N
        btnAdd.setText("Add New");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnReset.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/btn_reset.png"))); // NOI18N
        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        btnChange.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/Update.png"))); // NOI18N
        btnChange.setText("Change");
        btnChange.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChangeActionPerformed(evt);
            }
        });

        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/1402493873_save.png"))); // NOI18N
        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(144, Short.MAX_VALUE)
                .addComponent(btnAdd)
                .addGap(18, 18, 18)
                .addComponent(btnChange, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnSave)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnDel)
                .addGap(18, 18, 18)
                .addComponent(btnReset)
                .addGap(18, 18, 18)
                .addComponent(btnOk)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnOk)
                .addComponent(btnDel)
                .addComponent(btnAdd)
                .addComponent(btnReset)
                .addComponent(btnChange, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("List Position"));

        tbPosition.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Position", "Bonus Salary", "Description"
            }
        ));
        tbPosition.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbPositionMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(tbPosition);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 779, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 264, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
                    .addContainerGap()))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtPosNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPosNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPosNameActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        txtPosName.setEditable(true);
        txtDes.setEditable(true);
        txtBonus.setEditable(true);
        btnAdd.setEnabled(false);
        btnChange.setEnabled(false);
        btnDel.setEnabled(false);
        btnSave.setText("Save Add");
        txtPosName.setText("");
        txtBonus.setText("");
        txtDes.setText("");
        
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnChangeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangeActionPerformed
     int n = tbPosition.getSelectedRow();
       if(n < 0){
           JOptionPane.showMessageDialog(null, "Please choose row to update");
           return;
       }else{
           txtPosName.setEditable(true);
           txtDes.setEditable(true);
           txtBonus.setEditable(true);
           btnAdd.setEnabled(false);
           btnDel.setEnabled(false);
           btnSave.setText("Save Update");
           btnChange.setEnabled(false);
       }
    }//GEN-LAST:event_btnChangeActionPerformed

    private void btnDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDelActionPerformed
       int c = tbPosition.getSelectedRow();
     if(c<0){
         JOptionPane.showMessageDialog(null, "Please choose row to delete!");
     }else{
         int r = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete this row?","Notify",JOptionPane.YES_NO_OPTION);
         if(r == JOptionPane.OK_OPTION){
             int id = Integer.parseInt(tbPosition.getValueAt(c, 0).toString());
             if(new PositionList().delete(id) == true){
                 JOptionPane.showMessageDialog(null, "Delete successfull!");
                 modelPos = (DefaultTableModel) tbPosition.getModel();
                 modelPos.setRowCount(0);
                 for (PositionEntity item : new PositionList().getAllPosition()) {
                     modelPos.addRow(item.toVector());
                 }
                 return;
             }else{
                 JOptionPane.showMessageDialog(null, "Cannot delete this row !!");
                 return;
             }
         }
     }
    }//GEN-LAST:event_btnDelActionPerformed

    private void tbPositionMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbPositionMousePressed
        int e = tbPosition.getSelectedRow();
        txtPosName.setText(tbPosition.getValueAt(e, 1).toString());
        txtDes.setText(tbPosition.getValueAt(e, 2).toString());
        txtBonus.setText(tbPosition.getValueAt(e, 3).toString());
      
    }//GEN-LAST:event_tbPositionMousePressed

    private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkActionPerformed
int n =  tbPosition.getSelectedRow();
        if(n < 0){
            JOptionPane.showMessageDialog(null, "Please choose row to update!");
            return;
        }else{
            txtPosName.setEditable(true);
            txtDes.setEditable(true);
            btnAdd.setEnabled(false);
            btnDel.setEnabled(false);
            btnOk.setText("Save Update");
            btnChange.setEnabled(false);
            dispose();
        }
    }//GEN-LAST:event_btnOkActionPerformed

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
       txtPosName.setText("");
       txtBonus.setText("");
       txtDes.setText("");
    }//GEN-LAST:event_btnResetActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        if(btnSave.getText().equals("Save Add")){
            String name = txtPosName.getText().trim();
            String bonus = txtBonus.getText().trim();
            String description = txtDes.getText().trim();
            if(name.isEmpty()){
                JOptionPane.showMessageDialog(null, "Position name cannot be blank !");
                txtPosName.requestFocus();
                return;
            }
               if(txtPosName.getText().trim().matches("^[\\d]+$")){
                JOptionPane.showMessageDialog(null, "Position name cannot contain numberic");
                txtPosName.requestFocus();
                return;
            }
                 if(name.length() > 30){
                JOptionPane.showMessageDialog(null, "Position name must be less than 30 characters!");
                txtPosName.requestFocus();
                return;
            }
             
              if(description.isEmpty()){
                JOptionPane.showMessageDialog(null, "Description field cannot be blank !");
                txtPosName.requestFocus();
                return;
            }
            if(txtDes.getText().trim().matches("^[\\d]+$")){
                JOptionPane.showMessageDialog(null, "Description cannot contian numberic");
                txtDes.requestFocus();
                return;
            }
            if(bonus.isEmpty()){
                JOptionPane.showMessageDialog(null, "Bonus field cannot be blank !");
                txtPosName.requestFocus();
                return;
            }
            if(bonus.length() > 9){
                JOptionPane.showMessageDialog(null, "Bonus Salary  must be less than 9 characters!");
                txtPosName.requestFocus();
                return;
            }
             if(!txtBonus.getText().trim().matches("^[\\d]+$")){
                  JOptionPane.showMessageDialog(null, "Bonus must be Numberic");
                txtBonus.requestFocus();
                return;
            
             }
         
            
            PositionEntity p = new PositionEntity();
            p.setPositionName(txtPosName.getText().trim());
            p.setDescription( txtDes.getText().trim());
            
            PositionList pl = new PositionList();
            if(pl.insert(p)==true){
                JOptionPane.showMessageDialog(null, "Insert successfull!");
                modelPos = (DefaultTableModel) tbPosition.getModel();
                modelPos.setRowCount(0);
                for (PositionEntity item : new PositionList().getAllPosition()) {
                    modelPos.addRow(item.toVector());
                }
                btnAdd.setEnabled(true);
                btnChange.setEnabled(true);
                btnDel.setEnabled(true);
            }else{
                JOptionPane.showMessageDialog(null, "Insert failed!");
                return;
            }
            
        }
        
        if(btnSave.getText().equals("Save Update")){
            int row = tbPosition.getSelectedRow();
            String a = tbPosition.getValueAt(row, 0).toString(); 
            int ID = Integer.parseInt(a);
            
            PositionEntity p = new PositionEntity();
                p.setPositionId(ID);
                p.setPositionName(txtPosName.getText().trim());
                p.setDescription(txtDes.getText().trim());
                p.setBonusSalary(new BigDecimal(Integer.parseInt(txtBonus.getText().trim())));
            
            PositionList pl = new PositionList();
            if (pl.update(p) == true) {
                JOptionPane.showMessageDialog(null, "Update successfull");
                modelPos = (DefaultTableModel) tbPosition.getModel();
                modelPos.setRowCount(0);
                for (PositionEntity item : new PositionList().getAllPosition()) {
                    modelPos.addRow(item.toVector());
                } 
                btnAdd.setEnabled(true);
                btnChange.setEnabled(true);
                btnDel.setEnabled(true);
                
            } else {
                JOptionPane.showMessageDialog(null, "Update failed");
                return;
            }
        }
        
    }//GEN-LAST:event_btnSaveActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnChange;
    private javax.swing.JButton btnDel;
    private javax.swing.JButton btnOk;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tbPosition;
    private javax.swing.JTextField txtBonus;
    private javax.swing.JTextArea txtDes;
    private javax.swing.JTextField txtPosName;
    // End of variables declaration//GEN-END:variables
DefaultTableModel modelPos;
    
    private void initControl() {
        modelPos = (DefaultTableModel)tbPosition.getModel();
        PositionList  pl = new PositionList();
//        JOptionPane.showMessageDialog(null, pl.getAllPosition().size());
        for (PositionEntity item : pl.getAllPosition()) {
            modelPos.addRow(item.toVector());
}
    }
}
    
